package com.migu.contacts;

import android.app.Activity;
import android.content.Intent;
import android.util.Log;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.blankj.utilcode.util.ToastUtils;
import com.frank.libandroid.BaseApplication;
import com.frank.libandroid.GsonUtil;
import com.frank.libandroid.HttpCallback;
import com.frank.libandroid.HttpConverter;
import com.frank.libandroid.ReflectUtil;
import com.migu.contacts.login.LoginActivity;
import com.migu.contacts.net.Result;
import com.migu.contacts.util.UMHelper;
import com.migu.contacts.util.UserCenter;

import java.io.IOException;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.HashSet;
import java.util.Set;

import okhttp3.ResponseBody;

public class App extends BaseApplication {
    @Override
    public void onCreate() {
        super.onCreate();
        UMHelper.init(this);
        HttpCallback.converter = new HttpConverter() {
            @Override
            public <T> T convert(ResponseBody responseBody, HttpCallback<T> callback) {
                try {
                    String string = responseBody.string();
                    Type generic = ReflectUtil.getGeneric(callback, 0);
                    ParameterizedType parameterizedType = new ParameterizedType() {
                        @NonNull
                        @Override
                        public Type[] getActualTypeArguments() {
                            return new Type[]{generic};
                        }

                        @NonNull
                        @Override
                        public Type getRawType() {
                            return Result.class;
                        }

                        @Nullable
                        @Override
                        public Type getOwnerType() {
                            return null;
                        }
                    };
                    Result result = GsonUtil.fromJson(string, parameterizedType);
                    if (result.code == 200) {
                        return (T) result.data;
                    } else {
                        if (result.code == 401) {
                            UserCenter.logout();
                            if (!UserCenter.isLogging) {
                                UserCenter.isLogging = true;
                                Intent intent = new Intent(App.this, LoginActivity.class);
                                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                                startActivity(intent);
                            }
                        }
                        callback.onError(result.code, result.message);
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
                return null;
            }
        };
        HttpCallback.errorHandler = new HttpCallback.ErrorHandler() {
            @Override
            public void onError(int errorCode, String errorMessage) {
                Log.i(TAG, "onError: " + Thread.currentThread());
                if (errorCode != 401) {
                    ToastUtils.showShort(errorMessage);
                } else {
                    ToastUtils.showShort("重新登录");
                }
            }
        };
        UserCenter.init();
    }


    Set<Activity> activities = new HashSet<>();
    private static final String TAG = "App";
}
